<?php
/*
Plugin Name: Morgan's Replacement Links Widget
Plugin URI: http://tech.windsofstorm.net/wordpress/drop-links-widget/
Description: Replacement to allow for greater customization of the Links Widget.
Version: 0.7
Author: Alan Morgan
Author URI: http://tech.windsofstorm.net
*/

function widget_links2_init() {
	function widget_links2($args, $number = 1) {
		extract($args);
		$options	= get_option('widget_links');
		$title		= $options[$number]['title'];
		if (empty($options[$number]['title'])) {
			$title	= __('Links');
		}
		$use_drop	= $options[$number]['use_drop']		? '1'				: '0';
		$skip_head	= $options[$number]['skip_head']	? '1'				: '0';
		$new_window	= $options[$number]['new_window']	? '1'				: '0';
		$category	= $options[$number]['category'];
		$sort		= $options[$number]['sort'];
		if ($sort == '') {
			$sort	= 'name';
		}
	?>
			<?php echo $before_widget; ?>
				<?php if (!$skip_head) { ?>
				<?php echo $before_title . apply_filters('the_title', $title) . $after_title; ?>
				<ul>
				<?php } ?>
				<?php if ($use_drop) { ?>
				<li>
				<script language=javascript>
					function followLink (link, new_window) {
						if (link != '') {
							if (new_window == 0) {
								window.location = link
							} else {
								window.open(link, 'mrl_newwin')
							}
						}
					}
				</script>
				<form id="linksform_<?php echo $number; ?>" action="">
				<select name="links_list" onchange="followLink(document.forms.linksform_<?php echo $number; ?>.links_list[document.forms.linksform_<?php echo $number; ?>.links_list.selectedIndex].value, <?php echo $new_window; ?>);">
				<option value=''>Select Link</option>
				<?php
					$link_args	= "echo=0&show_images=0&orderby=$sort&before=&after=&category_before=<option value=''></option><option value=''>&category_after=</option>";
					if ($category) {
						$link_args	.= "&category=$category";
					}
					$links_list	= wp_list_bookmarks($link_args);
					$links_list	= eregi_replace("a href", "option value", $links_list);
					$links_list	= eregi_replace("a><br /", "option", $links_list);
					print($links_list);
				?>
				</select>
				</form></li>
				<?php } else  {
					$link_args	= "echo=0&show_images=0&orderby=$sort";
					if ($category) {
						$link_args	.= "&category=$category&category_before=&category_after=";
					}
					$links_list	= wp_list_bookmarks($link_args);
					if ($new_window) {
						$links_list	= eregi_replace("a href", "a target=_blank href", $links_list);
					}
					print($links_list);
				} ?>
				<?php if (!$skip_head) { ?>
				</ul>
				<?php } ?>
			<?php echo $after_widget; ?>
	<?php
	}

	function widget_links2_control($number) {
		$options = $newoptions = get_option('widget_links');
		if ( !is_array($options) )
                	$options = $newoptions = array();
		if ( $_POST["links-submit-$number"] ) {
			$newoptions[$number]['title']		= strip_tags(stripslashes($_POST["links-title-$number"]));
			$newoptions[$number]['use_drop']	= isset($_POST["links-use_drop-$number"]);
			$newoptions[$number]['skip_head']	= isset($_POST["links-skip_head-$number"]);
			$newoptions[$number]['category']	= $_POST["links-category-$number"];
			$newoptions[$number]['sort']		= $_POST["links-sort-$number"];
			$newoptions[$number]['new_window']	= isset($_POST["links-new_window-$number"]);
		}
		if ( $options != $newoptions ) {
			$options = $newoptions;
			update_option('widget_links', $options);
		}
		$title		= htmlspecialchars($options[$number]['title'], ENT_QUOTES);
		$use_drop	= $options[$number]['use_drop'] ? 'checked="checked"' : '';
		$skip_head	= $options[$number]['skip_head'] ? 'checked="checked"' : '';
		$new_window	= $options[$number]['new_window'] ? 'checked="checked"' : '';
		$link_cat	= $options[$number]['category']; // ? $options[$number]['category'] : 'none';
		$sort		= $options[$number]['sort']; // ? $options[$number]['sort'] : 'none';
		$cat_arr	= get_categories("type=link");
		$cat_keys	= array_keys($cat_arr);

		$cat_list	= "<option value=''>All Categories</option>\n";
		foreach ($cat_keys as $k) {
			$thisCat	= $cat_arr[$k];
			$cat_ID		= $thisCat->cat_ID;
			$cat_name	= $thisCat->cat_name;

			$cat_list	.= "<option value='$cat_ID'";
			if ($cat_ID == $link_cat) {
				$cat_list	.= " SELECTED";
			}
			$cat_list	.= "> $cat_name</option>\n";
		}

		$sort_types	= array('name','id','url','description','rating');
		foreach ($sort_types as $s) {
			$sort_list	.= "<option value='$s'";
			if ($sort == $s) {
				$sort_list	.= " SELECTED";
			}
			$sort_list	.= "> ". ucfirst($s) . "</option>\n";
		}
	?>
				<p><label for="links-title-<?php echo $number; ?>"><?php _e('Title:'); ?> <input style="width: 250px;" id="links-title" name="links-title-<?php echo $number; ?>" type="text" value="<?php echo $title; ?>" /></label></p>
				<p style="text-align:right;margin-right:40px;"><label for="links-use_drop-<?php echo $number; ?>"><?php _e('Use drop down list?'); ?> <input class="checkbox" type="checkbox" <?php echo $use_drop; ?> id="links-use_drop-<?php echo $number; ?>" name="links-use_drop-<?php echo $number; ?>" /></label></p>
				<p style="text-align:right;margin-right:40px;"><label for="links-skip_head-<?php echo $number; ?>"><?php _e('Skip header?'); ?> <input class="checkbox" type="checkbox" <?php echo $skip_head; ?> id="links-skip_head-<?php echo $number; ?>" name="links-skip_head-<?php echo $number; ?>" /></label></p>
				<p style="text-align:right;margin-right:40px;"><label for="links-new_window-<?php echo $number; ?>"><?php _e('Open in a new window?'); ?> <input class="checkbox" type="checkbox" <?php echo $new_window; ?> id="links-new_window-<?php echo $number; ?>" name="links-new_window-<?php echo $number; ?>" /></label></p>
				<p style="text-align:right;margin-right:40px;"><label for="links-category-<?php echo $number; ?>"><?php _e('Select category for links:'); ?><select name="links-category-<?php echo "$number"; ?>"><?php echo $cat_list; ?></select></label></p>
				<p style="text-align:right;margin-right:40px;"><label for="links-sort-<?php echo $number; ?>"><?php _e('Select sort order for links:'); ?><select name="links-sort-<?php echo "$number"; ?>"><?php echo $sort_list; ?></select></label></p>
				<input type="hidden" id="links-submit" name="links-submit-<?php echo $number; ?>" value="1" />
	<?php
	}

	function widget_links2_setup() {
        	$options = $newoptions = get_option('widget_links');
        	if ( isset($_POST['links-number-submit']) ) {
                	$number = (int) $_POST['links-number'];
                	if ( $number > 9 ) $number = 9;
                	if ( $number < 1 ) $number = 1;
                	$newoptions['number'] = $number;
        	}
        	if ( $options != $newoptions ) {
                	$options = $newoptions;
                	update_option('widget_links', $options);
                	widget_links2_register($options['number']);
        	}
	}

	function widget_links2_page() {
        	$options = $newoptions = get_option('widget_links');
	?>
        	<div class="wrap">
                	<form method="POST">
                        	<h2><?php _e('Links Widgets', 'widgets'); ?></h2>
                        	<p style="line-height: 30px;"><?php _e('How many links widgets would you like?', 'widgets'); ?>
                        	<select id="links-number" name="links-number" value="<?php echo $options['number']; ?>">
	<?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
                        	</select>
                        	<span class="submit"><input type="submit" name="links-number-submit" id="links-number-submit" value="<?php _e('Save'); ?>" /></span></p>
                	</form>
        	</div>
	<?php
	}

	function widget_links2_register() {
        	$options = get_option('widget_links');
        	$number = $options['number'];
        	if ( $number < 1 ) $number = 1;
        	if ( $number > 9 ) $number = 9;
		unregister_sidebar_widget('Links');
        	for ($i = 1; $i <= 9; $i++) {
                	$name = array('Morgan\'s Links %s', 'widgets', $i);
                	register_sidebar_widget($name, $i <= $number ? 'widget_links2' : /* unregister */ '', $i);
                	register_widget_control($name, $i <= $number ? 'widget_links2_control' : /* unregister */ '', 350, 230, $i);
        	}
        	add_action('sidebar_admin_setup', 'widget_links2_setup');
        	add_action('sidebar_admin_page', 'widget_links2_page');
	}

	widget_links2_register();
}

// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded', 'widget_links2_init');

?>
